import Vue from 'vue'
import Router from 'vue-router'
import {getToken} from '@/utils/auth'


Vue.use(Router);

const whitePage = ['/login'];

const sysRoutes = [
  {
    path: '/index',
    component: () => import('@/views/index.vue'),
    name: 'index',
    redirect: '/home/userInfo',
    children: [
      {
        path: '/home/userInfo',
        component: () => import('@/views/user/userInfo.vue'),
        name: 'userInfo',
      },
      {
        path: '/home/userOnline',
        component: () => import('@/views/user/userOnline.vue'),
        name: 'userOnline',
      },
      {
        path: '/home/addRoleManage',
        component: () => import('@/views/role/addRoleManage.vue'),
        name: 'addRoleManage',
      }, {
        path: '/home/AppIndex',
        component: () => import('@/views/app/AppIndex.vue'),
        name: 'AppIndex',
      }
    ]
  }
];

export const baseRoutes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: () => import('@/views/login.vue'),
    name: 'Login'
  },
  ...sysRoutes
];

const createRouter = () => new Router({
  routes: baseRoutes
});

const router = createRouter();
export default router


router.beforeEach((to, from, next) => {
  if (whitePage.includes(to.fullPath) || to.fullPath.startsWith('/login')) { // 白名单页面直接放行
    next()
  } else {
    if (getToken()) {
      next()
    } else {
      next('/login')
    }
  }
});
